/**
 * @(#)InterceptInfoServiceImpl.java 2016年4月20日
 * Copyright(C) 2015 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IInterceptInfoDAO;
import org.jsbd.boss.domian.InterceptInfo;
import org.jsbd.boss.service.IInterceptInfoService;
import org.jsbd.boss.util.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jodd.util.StringUtil;

/**
 * @author <a href="mailto:luoyaxi@zhexinit.com" >罗雅锡</a>
 * @version 1.0.0
 */
@Service("interceptInfoService")
public class InterceptInfoServiceImpl implements IInterceptInfoService {

	@Resource
	private RedisClient redisClient;
	@Autowired
	private IInterceptInfoDAO interceptInfoDAO;

	public int saveIntercept(InterceptInfo form) {
		return interceptInfoDAO.insertInterceptInfo(form);
	}

	public int modifyIntercept(InterceptInfo form) {
		return interceptInfoDAO.updateInterceptInfo(form);
	}

	public InterceptInfo findInterceptInfo(InterceptInfo form) {
		return interceptInfoDAO.findInterceptInfo(form);
	}

	public Map<String, Object> findInterceptInfoPageList(DataGridModel page, InterceptInfo form) {
		return interceptInfoDAO.selectInterceptInfoPageListMap(page, form);
	}

	@Override
	public InterceptInfo findIfInterceptExist(InterceptInfo form) {
		return interceptInfoDAO.findIfInterceptExist(form);
	}

	@Override
	public List<InterceptInfo> findInterceptInfoList(InterceptInfo form) {
		return interceptInfoDAO.selectInterceptInfoList(form);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsbd.boss.service.IInterceptInfoService#selectInterceptOfIds(java
	 * .lang.String, java.lang.String)
	 */
	@Override
	public List<InterceptInfo> selectInterceptOfIds(String mark, String interfaceIds) {
		if (StringUtil.isBlank(mark) || StringUtil.isBlank(interfaceIds)) {
			return null;
		}
		List<InterceptInfo> result = (List<InterceptInfo>) redisClient.get(CacheKey.CHANNEL_MARK_INTERCEPTINFO_KEY + mark);
		if (result == null) {
			result = new ArrayList<InterceptInfo>();
			String[] interceptIds = interfaceIds.split(",");
			if (interceptIds != null && interceptIds.length > 0) {
				for (String id : interceptIds) {
					InterceptInfo interceptinfo = interceptInfoDAO.findInterceptInfoByid(id);
					if (interceptinfo != null) {
						result.add(interceptinfo);
					}
				}
			}
			redisClient.put(CacheKey.CHANNEL_MARK_INTERCEPTINFO_KEY + mark, result, CacheKey.MEMCACHE_EXPIRY_TIME);
		}
		return result;

	}

	@Override
	public InterceptInfo selectInterceptOfId(String id) {
		String key = CacheKey.CHANNEL_MARK_INTERCEPTINFO_KEY + id;
		InterceptInfo interceptInfo = (InterceptInfo) redisClient.get(key);
		if (interceptInfo != null) {
			return interceptInfo;
		}
		InterceptInfo interceptinfo = interceptInfoDAO.findInterceptInfoByid(id);
		if (interceptinfo != null) {
			redisClient.put(key, interceptinfo, 300);
		}
		return interceptinfo;

	}

}
